fn main() {
    let mut list = List::new();
    list = list.prepend(1);
    list = list.prepend(2);
    list = list.prepend(3);
    println!("linked list has length: {}", list.len());
    println!("{}", list.stringify());
    // linked list has length: 3
    // 3, 2, 1, Nil
}

enum List {
    // 元组结构体，包含链表的一个元素和一个窒息那个下一个节点的指针
    Ele(u32, Box<List>),
    Nil,
}
impl List {
    fn new() -> List {
        List::Nil
    }
    fn prepend(self, elem: u32) -> List {
        List::Ele(elem, Box::new(self))
    }
    fn len(&self) -> u32 {
        match *self {
            List::Ele(_, ref tail) => 1 + tail.len(),
            List::Nil => 0,
        }
    }
    fn stringify(&self) -> String {
        match *self {
            List::Ele(head, ref tail) => {
                format!("{}, {}", tail.stringify(), head)
            }
            List::Nil => {
                format!("Nil")
            }
        }
    }
}
